/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.ad.domain.SysCommonName;
import cn.ibizlab.core.ad.service.SysCommonNameService;
import cn.ibizlab.core.ad.filter.SysCommonNameSearchContext;

/**
 * 实体[SysCommonName] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysCommonNameResource {

    @Autowired
    public SysCommonNameService sysCommonNameService;

    @Autowired
    @Lazy
    public SysCommonNameDTOMapping sysCommonNameDtoMapping;

    @Autowired
    @Lazy
    public SysCommonNameFilterDTOMapping sysCommonNameFilterDtoMapping;


    /**
    * 创建Create CN
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysCommonNameDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"CN" },  notes = "SysCommonName-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Create-all')")
    @PostMapping(value = {"sys_common_names","sys_common_names"})
    public ResponseEntity<SysCommonNameDTO> create
            (@Validated @RequestBody SysCommonNameDTO dto) {
        SysCommonName domain = sysCommonNameDtoMapping.toDomain(dto);
        sysCommonNameService.create(domain);
        SysCommonName rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysCommonNameDtoMapping.toDto(rt));
    }

    /**
    * 获取Get CN
    * 
    *
    * @param id id
    * @return ResponseEntity<SysCommonNameDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"CN" },  notes = "SysCommonName-Get ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Get-all')")
    @GetMapping("sys_common_names/{id}")
    public ResponseEntity<SysCommonNameDTO> getById
            (@PathVariable("id") String id) {
        SysCommonName rt = sysCommonNameService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysCommonNameDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove CN
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"CN" },  notes = "SysCommonName-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Remove-all')")
    @DeleteMapping("sys_common_names/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysCommonNameService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update CN
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysCommonNameDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"CN" },  notes = "SysCommonName-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Update-all')")
    @PutMapping("sys_common_names/{id}")
    public ResponseEntity<SysCommonNameDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysCommonNameDTO dto) {
        SysCommonName domain = sysCommonNameDtoMapping.toDomain(dto);
        domain.setId(id);
        sysCommonNameService.update(domain);
        SysCommonName rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysCommonNameDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey CN
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"CN" },  notes = "SysCommonName-CheckKey ")
    @PostMapping("sys_common_names/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysCommonNameDTO dto) {
        SysCommonName domain = sysCommonNameDtoMapping.toDomain(dto);
        Integer rt = sysCommonNameService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft CN
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysCommonNameDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"CN" },  notes = "SysCommonName-GetDraft ")
    @GetMapping("sys_common_names/getdraft")
    public ResponseEntity<SysCommonNameDTO> getDraft
            (@SpringQueryMap SysCommonNameDTO dto) {
        SysCommonName domain = sysCommonNameDtoMapping.toDomain(dto);
        SysCommonName rt = sysCommonNameService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysCommonNameDtoMapping.toDto(rt));
    }

    /**
    * 保存Save CN
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysCommonNameDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"CN" },  notes = "SysCommonName-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Save-all')")
    @PostMapping("sys_common_names/save")
    public ResponseEntity<SysCommonNameDTO> save
            (@Validated @RequestBody SysCommonNameDTO dto) {
        SysCommonName domain = sysCommonNameDtoMapping.toDomain(dto);
        sysCommonNameService.save(domain);
        SysCommonName rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysCommonNameDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchAuth CN
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysCommonNameDTO>>
    */
    @ApiOperation(value = "查询FetchAuth", tags = {"CN" },  notes = "SysCommonName-FetchAuth ")
    @PostMapping("sys_common_names/fetchauth")
    public ResponseEntity<List<SysCommonNameDTO>> fetchAuth
            (@Validated @RequestBody SysCommonNameFilterDTO dto) {
        SysCommonNameSearchContext context = sysCommonNameFilterDtoMapping.toDomain(dto);
        Page<SysCommonName> domains = sysCommonNameService.searchAuth(context) ;
        List<SysCommonNameDTO> list = sysCommonNameDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchDefault CN
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysCommonNameDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"CN" },  notes = "SysCommonName-FetchDefault ")
    @PostMapping("sys_common_names/fetchdefault")
    public ResponseEntity<List<SysCommonNameDTO>> fetchDefault
            (@Validated @RequestBody SysCommonNameFilterDTO dto) {
        SysCommonNameSearchContext context = sysCommonNameFilterDtoMapping.toDomain(dto);
        Page<SysCommonName> domains = sysCommonNameService.searchDefault(context) ;
        List<SysCommonNameDTO> list = sysCommonNameDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建CN
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Create-all')")
    @ApiOperation(value = "批量新建CN", tags = {"CN" },  notes = "批量新建CN")
	@PostMapping("sys_common_names/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysCommonNameDTO> dtos) {
        sysCommonNameService.createBatch(sysCommonNameDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除CN
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Remove-all')")
    @ApiOperation(value = "批量删除CN", tags = {"CN" },  notes = "批量删除CN")
	@DeleteMapping("sys_common_names/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysCommonNameService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新CN
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Update-all')")
    @ApiOperation(value = "批量更新CN", tags = {"CN" },  notes = "批量更新CN")
	@PutMapping("sys_common_names/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysCommonNameDTO> dtos) {
        sysCommonNameService.updateBatch(sysCommonNameDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存CN
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Save-all')")
    @ApiOperation(value = "批量保存CN", tags = {"CN" },  notes = "批量保存CN")
	@PostMapping("sys_common_names/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysCommonNameDTO> dtos) {
        sysCommonNameService.saveBatch(sysCommonNameDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入CN
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysCommonName-Save-all')")
    @ApiOperation(value = "批量导入CN", tags = {"CN" },  notes = "批量导入CN")
	@PostMapping("sys_common_names/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysCommonNameDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysCommonNameService.importData(config,ignoreError,sysCommonNameDtoMapping.toDomain(dtos)));
    }

}
